package com.louis.kitty.admin.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.louis.kitty.admin.dao.SysUserMapper;
import com.louis.kitty.admin.model.SysUserVo;
import com.louis.kitty.admin.model.User;
import com.louis.kitty.core.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.louis.kitty.admin.constants.SysConstants;
import com.louis.kitty.admin.model.SysUser;
import com.louis.kitty.admin.sevice.SysUserService;
import com.louis.kitty.admin.util.PasswordUtils;
import com.louis.kitty.core.http.HttpResult;
import com.louis.kitty.core.page.PageRequest;

/**
 * 用户控制器
 * @author Louis
 * @date Oct 29, 2018
 */
@RestController
@RequestMapping("user")
public class SysUserController {

	@Autowired
	private SysUserService sysUserService;



//	@PreAuthorize("hasAuthority('sys:user:add') AND hasAuthority('sys:user:edit')")
	@PostMapping(value="/save")
	public HttpResult save(@RequestBody SysUser record) {
		SysUser user = sysUserService.findById(record.getId());
		if(user != null) {
			if(SysConstants.ADMIN.equalsIgnoreCase(user.getName())) {
				return HttpResult.error("超级管理员不允许修改!");
			}
		}
		if(record.getPassword() != null) {
			String salt = PasswordUtils.getSalt();
			if(user == null) {
				// 新增用户
				if(sysUserService.findByName(record.getName()) != null) {
					return HttpResult.error("用户名已存在!");
				}
				String password = PasswordUtils.encode(record.getPassword(), salt);
				record.setSalt(salt);
				record.setPassword(password);
			} else {
				// 修改用户, 且修改了密码
				if(!record.getPassword().equals(user.getPassword())) {
					String password = PasswordUtils.encode(record.getPassword(), salt);
					record.setSalt(salt);
					record.setPassword(password);
				}
			}
		}
		return HttpResult.ok(sysUserService.save(record));
	}

@PreAuthorize("hasAuthority('sys:user:delete')")
@PostMapping(value="/delete")
public HttpResult delete(@RequestBody List<SysUser> records) {
	for(SysUser record:records) {
		SysUser sysUser = sysUserService.findById(record.getId());
		if(sysUser != null && SysConstants.ADMIN.equalsIgnoreCase(sysUser.getName())) {
			return HttpResult.error("超级管理员不允许删除!");
		}
	}
	return HttpResult.ok(sysUserService.delete(records));
}

	//	@PreAuthorize("hasAuthority('sys:user:view')")
	@GetMapping(value="/findByName")
		public HttpResult findByUserName(@RequestParam String name) {
			return HttpResult.ok(sysUserService.findByName(name));
	}

	// @PreAuthorize("hasAuthority('sys:user:view')")
	@GetMapping(value="/findPermissions")
	public HttpResult findPermissions(@RequestParam String name) {
		return HttpResult.ok(sysUserService.findPermissions(name));
	}

//	@PreAuthorize("hasAuthority('sys:user:view')")
	@GetMapping(value="/findUserRoles")
	public HttpResult findUserRoles(@RequestParam Long userId) {
		return HttpResult.ok(sysUserService.findUserRoles(userId));
	}

//	@PreAuthorize("hasAuthority('sys:user:view')")
	@PostMapping(value="/findPage")
	public HttpResult findPage(@RequestBody PageRequest pageRequest) {
		return HttpResult.ok(sysUserService.findPage(pageRequest));
	}

	// 查找机构id查找机构下所有的用户
	//@PreAuthorize("hasAuthority('sys:user:view')")
	@RequestMapping(value="/findUsersByDeptId",method = RequestMethod.GET)
	public HttpResult findUsersByDeptId(@RequestParam Long deptId) {
		List<SysUser> sysUsers=sysUserService.findUsersByDept(deptId);
		return HttpResult.ok(sysUsers);
	}

	@RequestMapping(value="/findUsersByRole",method = RequestMethod.GET)
	public HttpResult findUsersByRole(@RequestParam(value = "roleIds") List<Long> roleIds) {
		List<SysUser> sysUsers=sysUserService.findUsersByRole(roleIds);
		return HttpResult.ok(sysUsers);
	}

	@GetMapping(value="/findAllUsers")
	public HttpResult findAllUsers() {
		List<SysUserVo> sysUsers=sysUserService.findAllUsers();
		return HttpResult.ok(sysUsers);
	}


	@GetMapping(value="/findChargerByUserNo")
	public HttpResult findChargerByUserNo(@RequestParam(value = "no") String no) {
		User user=sysUserService.findChargerByUserNo(no);
		return HttpResult.ok(user);
	}

	@GetMapping(value="/findChargersByUserNo")
	public HttpResult findChargersByUserNo(@RequestParam(value = "no") String no) {
		Map users=sysUserService.findChargersByUserNo(no);
		return HttpResult.ok(users);
	}

	@GetMapping(value="/findUpperUserByUserNoIfNoCharge")
	public HttpResult findUpperUserByUserNoIfNoCharge(@RequestParam(value = "no") String no) {
		User user=sysUserService.findUpperUserByUserNoIfNoCharge(no);
		return HttpResult.ok(user);
	}

	@GetMapping(value="/findUpperByUserNoIfCharge")
	public HttpResult findUpperByUserNoIfCharge(@RequestParam(value = "no") String no) {
		User user=sysUserService.findUpperByUserNoIfCharge(no);
		return HttpResult.ok(user);
	}

	@GetMapping(value="/getCompanyId")
	public HttpResult getCompanyId(@RequestParam(value = "companyId") Integer id) {
		List<SysUser> list=sysUserService.findAllUsersByCompanyId(id);
		return HttpResult.ok(list);
	}


}
